'use strict';

const BaseController = require('../core/base_controller');
const model = 'SysAdmin'

class Controller extends BaseController {

    /**
     * GET
     * /api
     * 分页查询 page pageSize keyword
     */
    async index() {
        try {
            const result = await this.ctx.service.commonService.pagingQuery({...this.ctx.query, model: model});
            this.success(result);
        } catch (error) {
            this.error(error);
        }
    }

    /**
     * GET
     * /api/:id
     * 通过id查询
     */
    async show() {
        try {
            const result = await this.service.commonService.findByPk({id: this.ctx.params.id, model: model});
            this.success(result);
        } catch (error) {
            this.error(error);
        }
    }

    /**
     * DELETE
     * /api/:id
     * 通过id删除
     */
    async destroy() {
        try {
            const result = await this.service.commonService.delete({id: this.ctx.params.id, model: model});
            this.success(result);
        } catch (error) {
            this.error(error);
        }
    }

    /**
     * PUT
     * /api/:id
     */
    async update() {
        let id = this.ctx.params.id;
        let body = this.ctx.request.body;
        try {
            let result = await this.service.commonService.insertOrUpdate({data: {id, ...body}, model: model});
            this.success(result);
        } catch (error) {
            this.error(error);
        }
    }

    /**
     * POST
     * /api
     */
    async create() {
        let body = this.ctx.request.body;
        try {
            let result = await this.service.commonService.insertOrUpdate({data: body, model: model});
            this.success(result);
        } catch (error) {
            this.error(error);
        }
    }

    /**
     * 登录
     * @returns
     */
    async login() {
        const {ctx} = this;
        const {account, pwd} = ctx.request.body;

        const userInfo = await ctx.service.commonService.findByCol({
            findColName: 'account',
            value: account,
            model: model
        });

        if (userInfo === null) {
            this.error('用户不存在');
            return;
        }

        if (userInfo.pwd === pwd) {
            const Authorization = ctx.helper.encrypt(userInfo, '3600s'); // 创建 Authorization
            ctx.set('Authorization', Authorization); // 设置 Authorization
            let data = {last_ip: ctx.request.ip, last_time: new Date(), id: userInfo.id,}  // data
            ctx.service.commonService.insertOrUpdate({data, model: model}); // insertOrUpdate
            delete userInfo.id;
            delete userInfo.pwd;
            this.success({
                Authorization,
                account: userInfo.account
            }, '登录成功');
        } else {
            this.error('密码错误');
        }
    }

    /**
     * 登录状态
     * @returns
     */
    async status() {
        const {
            ctx
        } = this;
        const TOKEN = ctx.get('Authorization');
        let decrypt = ctx.helper.decrypt(TOKEN);
        delete decrypt.pwd;
        if (decrypt.token) {
            this.success(decrypt);
        } else {
            this.error('Authorization已失效');
        }
    }
}

module.exports = Controller;
